package com.frx.leetcode.classify.link;

/**
 * @author ：frx
 * @date ：Created in 2019/11/7 22:12
 * @description：ToDO
 * @modified By：
 * @version: ToDO
 */
public class MergeLink {

    public static void main(String[] args){

        ListNode n3 = new ListNode(4);
        ListNode n2 = new ListNode(2,n3);
        ListNode n1 = new ListNode(1,n2);
        printLink(n1);
        ListNode a3 = new ListNode(3);
        ListNode a2 = new ListNode(2,a3);
        ListNode a1 = new ListNode(1,a2);
        ListNode a0 = new ListNode(0,a1);
        System.out.println();
        printLink(a1);
        System.out.println();
        printLink(mergeTwoLink(a0,n1));

    }

    public ListNode reverseLink(ListNode head){
        return null;
    }


    public static ListNode mergeTwoLink(ListNode l1, ListNode l2){

        ListNode head = new ListNode(1);
        ListNode cur = head;
        while (l1 != null && l2!=null){
            if (l1.val < l2.val){
                cur.next = new ListNode(l1.val);
                l1 = l1.next;
            } else {
                cur.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            cur = cur.next;
        }
        while (l1 != null){
            cur.next = new ListNode(l1.val);
            l1 = l1.next;
            cur = cur.next;
        }

        while (l2 != null){
            cur.next = new ListNode(l2.val);
            l2 = l2.next;
            cur = cur.next;
        }

        return head.next;
    }


    public static void printLink(ListNode node){
        System.out.print(node.val);
        node = node.next;
        while (node != null){
            System.out.print("->" + node.val);
            node = node.next;
        }
    }
}

